import { DataBase } from './database.js'

export class AddressList {
  static queryAll() {
    return DataBase.read()
  }

  static query(id) {
    const data = DataBase.read()
    return data.find((item) => item.id === id)
  }

  static add(person) {
    const data = DataBase.read()
    data.push(person)
    DataBase.write(data)
  }

  static update(person) {
    const data = DataBase.read()
    DataBase.write(
      data.map((item) => {
        if (item.id === person.id) {
          return person
        }
        return item
      })
    )
  }

  static delete(id) {
    const data = DataBase.read()
    const newData = data.filter((item) => item.id !== id)
    DataBase.write(newData)
  }
}
